// pages/otherManagement/otherManagement.js
import Dialog from '@vant/weapp/dialog/dialog';
const http = require("../../api/camp")
const api = require("../../api/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    campId: "",
    campConditionDatas: [], //营位数据列表
    campScenicDatas: [], //景点数据列表
    campActivityDatas: [], //活动数据列表
    campOtherDatas: [], //项目数据列表
    campConditionList: [], //营位数据列表
    campScenicList: [], //景点数据列表
    campActivityList: [], //活动数据列表
    campOtherList: [], //项目数据列表
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.title}`,
      icon: 'none',
    });
  },
  // 景点
  addScenic() {
    // console.log(123);
    this.setData({
      campScenicStatus: true,
    })
  },
  // 获取周边景点
  getScenicInfo() {
    api.getSpots(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          campScenicList: res.data
        })
      }
      // console.log(res, "景点列表");
    })
  },
  // 删除景点
  delScenic(event) {
    Dialog.confirm({
        title: '周边景点删除',
        message: '确定删除该景点吗？',
      })
      .then(() => {
        http.campDelScenic(event.currentTarget.dataset.scenicId).then(res => {
          if (res.code == 200) {
            this.getScenicInfo()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  // 获取营位
  getCampCampsite() {
    api.getCampCampsite(this.data.campId).then(res => {
      console.log(res)
      if (res.code == 200 && res.data) {
        this.setData({
          campConditionList: res.data
        })
      }
      // console.log(res, "营位列表");
    })
  },
  // 营位
  toCampType() {
    this.setData({
      campCondotionStatus: true,
    })
  },
  //营位选择数据
  campConditionList(e) {
    this.setData({
      campConditionDatas: e.detail,
      touristConditionData: e.detail
    })
    // console.log(e);
    const campSite = this.data.campConditionDatas
    const index = campSite.length - 1
    // 添加
    // console.log(campSite[index].factorId.join(','));
    const result = campSite.map(item => {
      item.factor = item.factorId?.join(",")
      item.campId = this.data.campId
      return item
    })
    const object = result[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.campAddCampsite([result[index]]).then((res) => {
        // console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 1500
          })
          this.getCampCampsite()
        }
      })
    }

  },
  // 删除营位
  delCondition(event) {
    Dialog.confirm({
        title: '营位删除',
        message: '确定删除该营位吗？',
      })
      .then(() => {
        console.log(event.currentTarget)
        http.campDelCampsite(event.currentTarget.dataset.id).then(res => {
          console.log(res)
          if (res.code == 200) {
            this.getCampCampsite()
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  //景点数据
  campScenicList(e) {
    this.setData({
      campScenicDatas: e.detail,
      // touristConditionData: e.detail
    })
    console.log(e);
    const campScenic = this.data.campScenicDatas
    const index = campScenic.length - 1
    console.log(campScenic[index]);
    const object = campScenic[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      // 添加请求
      http.addScenic({
        ...campScenic[index],
        campId: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })
          this.getScenicInfo()
        }
      })
    }

  },
  // 获取项目
  getProject() {
    api.getProject(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          campOtherList: res.data
        })
      }
      // console.log(res, "项目列表");
    })
  },
  // 项目
  addOther() {
    // console.log(123);
    this.setData({
      campOtherStatus: true,
    })
  },
  //项目数据
  campOtherList(e) {
    this.setData({
      campOtherDatas: e.detail,
    })
    console.log(e);
    const campOther = this.data.campOtherDatas
    const index = campOther.length - 1
    // 添加请求
    const object = campOther[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.addOtherProject({
        ...campOther[index],
        campId: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })

          this.getProject()
        }
      })
    }

  },
  // 删除项目
  delOther(event) {
    Dialog.confirm({
        title: '项目删除',
        message: '确定删除该项目吗？',
      })
      .then(() => {
        http.campDelProject(event.currentTarget.dataset.id).then(res => {
          if (res.code == 200) {
            this.getProject()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  // 获取活动
  getActivity() {
    api.getActivity(this.data.campId).then(res => {
      if (res.code == 200 && res.data) {
        this.setData({
          campActivityList: res.data
        })
      }
      // console.log(res, "活动列表");
    })
  },
  // 活动
  addActivity() {
    // console.log(123);
    this.setData({
      campActivityStatus: true,
    })
  },
  //活动数据
  campActivityList(e) {
    this.setData({
      campActivityDatas: e.detail,
      // touristConditionData: e.detail
    })
    console.log(e);
    const campActivity = this.data.campActivityDatas
    const index = campActivity.length - 1
    // 添加请求
    const object = campActivity[index]
    var isEmpty = true;
    Object.keys(object).forEach(function (x) {
      if (object[x] === null || object[x] === "") {
        isEmpty = false;
      }
    });
    if (isEmpty) {
      http.addActivity({
        ...campActivity[index],
        campId: this.data.campId
      }).then((res) => {
        console.log(res);
        if (res.code === 200) {
          wx.showToast({
            title: '提交成功，待审核',
            icon: 'none',
            duration: 1500
          })
          this.getActivity()
        }
      })
    }
    
  },
  // 删除景点
  delActivity(event) {
    Dialog.confirm({
        title: '活动删除',
        message: '确定删除该活动吗？',
      })
      .then(() => {
        http.campDelActivity(event.currentTarget.dataset.id).then(res => {
          if (res.code == 200) {
            this.getActivity()
          }
        })
      })
      .catch(() => {
        // on cancel
      });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.campId);
    this.setData({
      campId: options.campId
    })

  },
  onShow() {
    this.getScenicInfo()
    this.getCampCampsite()
    this.getProject()
    this.getActivity()
  }


})